<template>
	<view class="container">
		<view class="left-bottom-sign"></view>
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">REGISTER</view>
			<view class="welcome">
				欢迎加入！
			</view>
			<view class="input-content">
				<view class="input-item">
					<text class="tit">用户名</text>
					<input type="text" v-model="username" placeholder="请输入用户名" maxlength="20"/>
				</view>
				<view class="input-item">
					<text class="tit">密码</text>
					<input type="text" v-model="password" placeholder="8-18位不含特殊字符的数字、字母组合" placeholder-class="input-empty" maxlength="20" password/>
				</view>
				<view class="input-item">
					<text class="tit">确认密码</text>
					<input type="text" v-model="confirmPassword" placeholder="请再次输入密码" placeholder-class="input-empty" maxlength="20" password/>
				</view>
				<view class="input-item">
					<text class="tit">手机号</text>
					<input type="number" v-model="telephone" placeholder="请输入手机号" maxlength="11"/>
				</view>
				<view class="input-item verification">
					<text class="tit">验证码</text>
					<input type="number" v-model="authCode" placeholder="请输入验证码" maxlength="6"/>
					<button class="verification-btn" @click="getAuthCode" :disabled="getCodeDisabled">
						{{getCodeText}}
					</button>
				</view>
			</view>
			<button class="confirm-btn" @click="toRegister" :disabled="registering">注册</button>
		</view>
		<view class="register-section">
			已有账号?
			<text @click="toLogin">去登录</text>
		</view>
	</view>
</template>

<script>
	import { API_BASE_URL } from '@/utils/appConfig.js';
	
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: '',
				telephone: '',
				authCode: '',
				registering: false,
				getCodeDisabled: false,
				getCodeText: '获取验证码',
				countDown: 60
			}
		},
		onLoad() {
		},
		methods: {
			navBack() {
				uni.navigateBack();
			},
			toLogin() {
				uni.navigateTo({url:'/pages/public/login'});
			},
			getAuthCode() {
				// 手机号验证
				if(!/^1\d{10}$/.test(this.telephone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				
				// 禁用按钮，倒计时
				this.getCodeDisabled = true;
				this.countDown = 60;
				this.getCodeText = this.countDown + 's';
				
				let timer = setInterval(() => {
					this.countDown--;
					this.getCodeText = this.countDown + 's';
					
					if(this.countDown <= 0) {
						clearInterval(timer);
						this.getCodeDisabled = false;
						this.getCodeText = '获取验证码';
					}
				}, 1000);
				
				// 直接调用获取验证码接口
				uni.request({
					url: API_BASE_URL + '/sso/getAuthCode',
					method: 'GET',
					data: {
						telephone: this.telephone
					},
					success: (res) => {
						if (res.data && res.data.code === 200) {
							uni.showToast({
								title: '验证码发送成功',
								icon: 'none'
							});
						} else {
							uni.showToast({
								title: res.data.message || '验证码发送失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '验证码发送失败，请稍后重试',
							icon: 'none'
						});
					}
				});
			},
			toRegister() {
				// 表单验证
				if(this.username.length < 3) {
					uni.showToast({
						title: '用户名不能少于3个字符',
						icon: 'none'
					});
					return;
				}
				if(this.password.length < 6) {
					uni.showToast({
						title: '密码不能少于6位',
						icon: 'none'
					});
					return;
				}
				if(this.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'none'
					});
					return;
				}
				if(!/^1\d{10}$/.test(this.telephone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				if(this.authCode.length < 4) {
					uni.showToast({
						title: '请输入正确的验证码',
						icon: 'none'
					});
					return;
				}
				
				this.registering = true;
				
				// 直接使用uni.request调用注册接口，以表单格式发送数据
				uni.request({
					url: API_BASE_URL + '/sso/register',
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: this.username,
						password: this.password,
						telephone: this.telephone,
						authCode: this.authCode
					},
					success: (res) => {
						if (res.data && res.data.code === 200) {
							uni.showToast({
								title: '注册成功',
								icon: 'success'
							});
							
							// 注册成功后跳转到登录页
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/public/login'
								});
							}, 1500);
						} else {
							uni.showToast({
								title: res.data.message || '注册失败',
								icon: 'none'
							});
							this.registering = false;
						}
					},
					fail: () => {
						uni.showToast({
							title: '注册失败，请稍后重试',
							icon: 'none'
						});
						this.registering = false;
					}
				});
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 60upx;
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		padding: 0 30upx;
		background: $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 50upx;
		position: relative;

		&:last-child {
			margin-bottom: 0;
		}

		&.verification {
			padding-right: 260upx;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}

		.verification-btn {
			position: absolute;
			top: 50%;
			right: 30upx;
			transform: translateY(-50%);
			width: 200upx;
			height: 60upx;
			line-height: 60upx;
			background: $uni-color-primary;
			color: #fff;
			font-size: $font-sm;
			border-radius: 50px;
			z-index: 10;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}

	.forget-section {
		font-size: $font-sm+2upx;
		color: $font-color-spec;
		text-align: center;
		margin-top: 40upx;
	}

	.register-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;

		text {
			color: $uni-color-primary;
			margin-left: 10upx;
		}
	}
</style>
